package com.empire.module.member.controller.admin.internetarticle;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.internetarticle.vo.*;
import com.empire.module.member.dal.dataobject.internetarticle.InternetArticleDO;
import com.empire.module.member.service.internetarticle.InternetArticleService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 互联网文章")
@RestController
@RequestMapping("/member/internet-article")
@Validated
public class InternetArticleController {

    @Resource
    private InternetArticleService internetArticleService;

    @PostMapping("/create")
    @Operation(summary = "创建互联网文章")
    @PreAuthorize("@ss.hasPermission('member:internet-article:create')")
    public CommonResult<Long> createInternetArticle(@Valid @RequestBody InternetArticleSaveReqVO createReqVO) {
        return success(internetArticleService.createInternetArticle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新互联网文章")
    @PreAuthorize("@ss.hasPermission('member:internet-article:update')")
    public CommonResult<Boolean> updateInternetArticle(@Valid @RequestBody InternetArticleSaveReqVO updateReqVO) {
        internetArticleService.updateInternetArticle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除互联网文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:internet-article:delete')")
    public CommonResult<Boolean> deleteInternetArticle(@RequestParam("id") Long id) {
        internetArticleService.deleteInternetArticle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得互联网文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:internet-article:query')")
    public CommonResult<InternetArticleRespVO> getInternetArticle(@RequestParam("id") Long id) {
        InternetArticleDO internetArticle = internetArticleService.getInternetArticle(id);
        return success(BeanUtils.toBean(internetArticle, InternetArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得互联网文章分页")
//    @PreAuthorize("@ss.hasPermission('member:internet-article:query')")
    public CommonResult<PageResult<InternetArticleRespVO>> getInternetArticlePage(@Valid InternetArticlePageReqVO pageReqVO) {
        PageResult<InternetArticleDO> pageResult = internetArticleService.getInternetArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InternetArticleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出互联网文章 Excel")
    @PreAuthorize("@ss.hasPermission('member:internet-article:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInternetArticleExcel(@Valid InternetArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InternetArticleDO> list = internetArticleService.getInternetArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "互联网文章.xls", "数据", InternetArticleRespVO.class,
                        BeanUtils.toBean(list, InternetArticleRespVO.class));
    }

}